#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#if defined(_WIN32) || defined(__CYGWIN__)
// Windows (x86 or x64)
// ...
#elif defined(__linux__)
// Linux
// ...
#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS
// ...
#elif defined(unix) || defined(__unix__) || defined(__unix)
// Unix like OS
// ...
#else
#error Unknown environment!
#endif

_Noreturn void menu();
int random_generator();
int find_max(int *ptr, int *size);
int *init_array(int *n);
void select_sort(int *ptr, int *size);
void scan_array(int *p, int *n);
void print_array(int *ptr, int *n);
void swap(int *a, int *b);
void bubble_sort(int *ptr, int *n);
void field_array(int *p, int *n);
void ready_set_of_num(int *ptr, int *n);
//__attribute__((unused))
void read_from_file(int *ptr, int *size, char *filename);
int count_arr_size(char *filename);

int main(int argc, char *argv[]) {
    menu();
}

_Noreturn void menu() {
    int *ArraySize = 0, *ArrayPtr = NULL;

    while (1) {
        int ch;
        puts("chose filling method\n"
             "1 - auto generated\n"
             "2 - manually input\n"
             "3 - read from file\n"
             "4 - ready set for arr[10], arr[100]\n");
        scanf("%d", &ch);
        if (ch == 1 || ch == 2) {
            puts("input array size\n");
            scanf("%d", &ArraySize);
            ArrayPtr = init_array(&ArraySize);
            switch (ch) {
                case 1:
                    puts("generate");
                    field_array(ArrayPtr, ArraySize);
                    break;
                case 2:
                    puts("input");
                    scan_array(ArrayPtr, &ArraySize);
                    break;
                default:
                    break;
            }
        } else if (ch == 3) {
            char *filename = "file.txt";
            *ArraySize = count_arr_size(filename);
            ArrayPtr = init_array(ArraySize);
            //read_from_file(ArrayPtr, ArraySize, filename);
        } else if (ch == 4) {
            puts("chose arr size\n"
                 "1 - 10\n"
                 "2 - 100\n");
            scanf("%d", &ch);
            switch (ch) {
                case 1:
                    puts("arr[10]");
                    *ArraySize = 10;
                    ArrayPtr = init_array(&ArraySize);
                    ready_set_of_num(ArrayPtr, &ArraySize);
                    break;
                case 2:
                    puts("arr[100]");
                    *ArraySize = 100;
                    ArrayPtr = init_array(ArraySize);
                    ready_set_of_num(ArrayPtr, ArraySize);
                    break;
                default:
                    break;
            }
        }
        print_array(ArrayPtr, &ArraySize);
        select_sort(ArrayPtr, &ArraySize);
        puts("arr");
        print_array(ArrayPtr, &ArraySize);
    }
}

void select_sort(int *ptr, int *size) {
    int ch;
    puts("chose array sort\n"
         "1 - bubble\n"//1Сортировка пузырьковая
         "2 - \n"      //2Сортировка вставками
         "3 - \n"      //3Сортировка выбором
         "4 - \n"      //4Сортировка слиянием
         "5 - \n"      //5Быстрая сортировка
         "6 - \n"      //6Сортировка Шелла
         "7 - \n"      //7Пирамидальная сортировка
         "8 - ");      //8Поразрядная сортировка
    scanf("%d", &ch);
    switch (ch) {
        case 1:
            bubble_sort(ptr, size);
            break;
        case 2:
            printf("%d \n", find_max(ptr, size));
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            break;
        default:
            break;
    }
}

int find_max(int *ptr, int *size) {
    int max = ptr[0];
    for (int i = 0; i < *size; ++i) {
        if (max < ptr[i]) {
            max = ptr[i];
        }
    }
    return max;
}

void field_array(int *p, int *n) {
    random_generator();
}

void bubble_sort(int *ptr, int *n) {
    for (int j = 0; j < *n; j++) {
        for (int i = 0; i < *n - 1; i++) {
            if (ptr[i] > ptr[i + 1]) {
                swap(&ptr[i], &ptr[i + 1]);
            }
        }
    }
}

void read_from_file(int *ptr, int *size, char *filename) {
    FILE *fp;
    char c, f;
    if ((fp = fopen(filename, "r")) == NULL) {
        perror("Error occured while opening file");
    }
    int i = 0;
    while ((c = getc(fp)) != EOF) {
        if (c == 32) {
            i++;
        }
        ptr[i] = (int) c;
    }
    fclose(fp);
}

int count_arr_size(char *filename) {
    FILE *fp = fopen(filename, "r");
    int count = 1;
    char tmp;
    while ((tmp = getc(fp)) != EOF) {
        if (tmp == 32) {
            count++;
        }
    }
    fclose(fp);
    return count;
}

void scan_array(int *p, int *n) {
    for (int i = 0; i < *n; i++) {
        scanf("%d", &p[i]);
    }
}

void print_array(int *ptr, int *n) {
    for (int i = 0; i < *n; i++)
        printf("%d\n", ptr[i]);
}

int *init_array(int *n) {
    int *p = (int *) malloc(*n * sizeof(int));
    return p;
}

void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

int random_generator() {
    srand((unsigned) time(NULL));
    int Generate = rand() % 100;
    return Generate;
}

void ready_set_of_num(int *ptr, int *n) {
    int Arr10[10] = {1, 7, 10, 8, 6, 3, 9, 5, 2, 4},
        Arr100[100] = {75, 3, 36, 45, 13, 23, 39, 74, 17, 38,
                       69, 44, 96, 26, 90, 31, 54, 19, 56, 6,
                       40, 62, 57, 33, 76, 1, 73, 21, 89, 92,
                       16, 42, 35, 77, 60, 97, 28, 22, 79, 30,
                       66, 15, 14, 95, 9, 25, 11, 8, 72, 80,
                       55, 47, 18, 58, 91, 94, 65, 51, 48, 52,
                       86, 61, 81, 53, 32, 84, 5, 12, 59, 78,
                       82, 50, 83, 99, 37, 2, 7, 70, 24, 49,
                       93, 87, 29, 68, 71, 34, 67, 10, 98, 63,
                       100, 43, 4, 41, 46, 20, 85, 27, 88, 64};

    switch (*n) {
        case 10:
            for (int i = 0; i < *n; i++) {
                ptr[i] = Arr10[i];
            }
            break;
        case 100:
            for (int i = 0; i < *n; i++) {
                ptr[i] = Arr100[i];
            }
            break;
        default:
            break;
    }
}
